﻿#region Header

/***************************************************************************
Author : Krishan Kaliraman (www.sharedwith.me)  Date: 1 Aug 2012
***************************************************************************/

#endregion Header
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using Microsoft.TeamFoundation.Server;

namespace Common
{
    public class TfsUserFactory
    {
        private static Identity[] listOfTfsIdentities;
        private static List<TfsUser> lisOfTfsUsers;
       
        private TfsUserFactory()
        {
            listOfTfsIdentities = Helper.LoadTFSUsersandPrintThemOnPage();
            FillListOfTfsUsers(listOfTfsIdentities);
        }

        private void FillListOfTfsUsers(Identity[] listOfTfsIdentities)
        {
            lisOfTfsUsers = new List<TfsUser>();
            foreach (var currentIdentity in listOfTfsIdentities)
            {
                TfsUser newTfsUser = new TfsUser() 
                                      { 
                                        AccountName =currentIdentity .AccountName ,
                                        DisplayName =currentIdentity .DisplayName ,
                                        MailAddress =currentIdentity .MailAddress ,
                                        DistinguishedName =currentIdentity .DistinguishedName ,
                                        
                                      };
                lisOfTfsUsers.Add(newTfsUser);
            }
            lisOfTfsUsers = lisOfTfsUsers.OrderBy(tfsUser => tfsUser.DisplayName).ToList<TfsUser>();
        }
        
        public static List<TfsUser> CreateTFsUserList()
        {
            TfsUserFactory tfsUserFactory = new TfsUserFactory();
            List<TfsUser> tfsUsers = lisOfTfsUsers;
            List<TfsUser> tfsUsersWithMailAddress= tfsUsers.Where( t=> t.MailAddress!=string.Empty ).ToList<TfsUser>();
            foreach (string teamName in StaticItems.AllTeams.Keys)
            {
                TfsUser newDummyUserForEmailAddressOnly = new TfsUser() { DisplayName = teamName, MailAddress = StaticItems.AllTeams[teamName] };
                tfsUsersWithMailAddress.Add(newDummyUserForEmailAddressOnly);
            }
            return tfsUsersWithMailAddress;
        }

        public static List<TfsUser> CreateTFsUserList(NetworkCredential networkCredentials)
        {
            TfsUserFactory tfsUserFactory = new TfsUserFactory();
            List<TfsUser> tfsUsers = lisOfTfsUsers;
            List<TfsUser> tfsUsersWithMailAddress = tfsUsers.Where(t => t.MailAddress != string.Empty).ToList<TfsUser>();
            foreach(string teamName in StaticItems.AllTeams.Keys)
            {
                TfsUser newDummyUserForEmailAddressOnly=new TfsUser(){DisplayName = teamName, MailAddress = StaticItems.AllTeams[teamName]};
                tfsUsersWithMailAddress.Add(newDummyUserForEmailAddressOnly);
            }
            return tfsUsersWithMailAddress;
        }
    }
}
